<template>
  <!-- 系统管理 -->
  <div class="app-container">
    <div class="mainbox">
      <!-- 顶部操作 -->
      <div class="filter-container">
        <el-form ref="filterFormRef" :inline="true" :model="filterForm">
          <el-form-item label="楼宇名称:" prop="premisesName">
            <el-input
              v-model.trim="filterForm.premisesName"
              class="filter-item limit"
              style="width: 200px;margin-right:10px"
              maxlength="30"
              clearable
              placeholder="请输入楼宇名称"
              @keyup.enter.native="$_handleFilter"
            />
          </el-form-item>
          <el-form-item label="街道:" prop="premisesStreetCode">
            <el-select v-model.trim="filterForm.premisesStreetCode" placeholder="请选择" :clearable="true">
              <el-option
                v-for="dict in dict.type.zs_ctjd"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="招引业态:" prop="premisesFormatCode">
            <el-select v-model.trim="filterForm.premisesFormatCode" placeholder="请选择" :clearable="true">
              <el-option
                v-for="dict in dict.type.zs_zyyt"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <div>
            <el-form-item label="楼宇总面积(m²):" prop="premisesAreaMin" style="margin-right: 0;">
              <el-input
                style="width: 140px;"
                type="number"
                v-model.trim="filterForm.premisesAreaMin"
                class="filter-item limit"
                clearable
                @keyup.enter.native="$_handleFilter"
              />
            </el-form-item>
            <span style="display:inline-block;padding: 6px 8px;">-</span>
            <el-form-item label="" prop="premisesAreaMax">
              <el-input
                style="width: 140px;"
                type="number"
                v-model.trim="filterForm.premisesAreaMax"
                class="filter-item limit"
                clearable
                @keyup.enter.native="$_handleFilter"
              />
            </el-form-item>
          </div>
        </el-form>
        <div class="flex-1" />
        <el-button type="primary" @click="$_handleFilter">查询</el-button>
        <el-button @click="$_handleReset">重置</el-button>

      </div>
      <!-- 表格 -->
      <div class="table-container table-fullscreen">
        <div class="table-opt-container">
          <el-button
            v-permission="['UR_GEOGRAPHY_ADD_BUTTON']"
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
          >
            新增
          </el-button>
          <el-button
            v-permission="['UR_GEOGRAPHY_DOWNLOAD_BUTTON']"
            icon="el-icon-download"
            @click="handleExport('d')"
          >
            下载模板
          </el-button>
          <el-button
            v-permission="['UR_GEOGRAPHY_IMPORT_BUTTON']"
            icon="el-icon-folder-add"
            type="primary"
            @click="$_handleImport()"
          >
            导入
          </el-button>
          <div class="flex-1" />
          <dt-dialog-column :columns="columns" @queryTable="getList" />
        </div>
        <el-table
          ref="table"
          v-loading="MX_listLoading"
          :data="MX_list"
          border
          highlight-current-row
          :row-key="$_getRowKey"
          style="width: 100%;text-align:center"
          :header-cell-style="MX_headerCellStyle"
          @selection-change="$_handleSelectionChange"
        >
          <template slot="empty">
            <p>{{ $store.getters.dataText }}</p>
          </template>
          <el-table-column
            fixed="left"
            type="index"
            label="序号"
            width="70"
            align="center"
            :index="$_tableIndex"
          />
          <el-table-column
            v-for="item in columns"
            :key="item.prop"
            :prop="item.prop"
            :label="item.label"
            :width="item.width"
            :min-width="item.minWidth"
            :formatter="item.formatter"
            align="center"
          />
          <el-table-column
            :key="Math.random()"
            fixed="right"
            label="操作"
            width="220px"
            align="center"
          >
            <template slot-scope="scope">
              <el-button
                v-permission="['UR_GEOGRAPHY_DETAIL_BUTTON']"
                size="mini"
                class="tBtn"
                @click="$_handleDetail(scope.row)"
              >详情</el-button>
              <el-button
                v-permission="['UR_GEOGRAPHY_UPDATE_BUTTON']"
                size="mini"
                class="tBtn"
                @click="handleEdit(scope.row)"
              >修改</el-button>
              <el-button
                v-permission="['UR_GEOGRAPHY_DEL_BUTTON']"
                size="mini"
                type="danger"
                class="tBtn"
                @click="handleDelete(scope.row[idKeyName])"
              >删除</el-button>
              <el-button
                v-permission="['UR_GEOGRAPHY_RECORD_BUTTON']"
                size="mini"
                class="tBtn"
                @click="getEditRecordList(scope.row[idKeyName])"
              >修改记录</el-button>
              <el-button
                v-permission="['UR_GEOGRAPHY_REVIEW_BUTTON']"
                size="mini"
                class="tBtn"
                type="primary"
                @click="handleReview(scope.row)"
              >审核</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <dt-pagination
          v-show="MX_total > 0"
          :total="MX_total"
          :page.sync="MX_pageNav.pageNo"
          :limit.sync="MX_pageNav.pageSize"
          @pagination="getList"
        />
      </div>
      <DialogForm ref="dialogForm" labelWidth="140px" :formDesignList="formDesignList" />
      <dtDialog
        title="导入"
        class="get-page-form-import-dialog"
        :visible.sync="MX_fileUploadVisible"
        width="620px"
        :is-loading="MX_uploadLoading"
        @closeDialog="MX_fileUploadVisible = false"
        @comfirmBtn="$_importDataSave(importApi)"
      >
        <el-form slot="content">
          <div class="">
            <dt-importFile ref="dtImportFileRef" :down-load-template="handleImportTemplate" :type="fileName" />
          </div>
        </el-form>
      </dtDialog>
      <dtDialog
        title="修改记录"
        :footerSlot="true"
        class="get-page-form-import-dialog"
        :visible.sync="isOpenRecordPop"
        width="85%"
        @closeDialog="isOpenRecordPop = false"
      >
        <el-table
          ref="table"
          :data="editRecordList"
          border
          highlight-current-row
          :row-key="$_getRowKey"
          style="width: 100%;text-align:center"
          :header-cell-style="MX_headerCellStyle"
        >
          <template slot="empty">
            <p>暂无数据！</p>
          </template>
          <el-table-column
            fixed="left"
            type="index"
            label="序号"
            width="70"
            align="center"
            :index="$_tableIndex"
          />
          <el-table-column
            v-for="item in editRecordColumns"
            :key="item.prop"
            :prop="item.prop"
            :label="item.label"
            :width="item.width"
            :min-width="item.minWidth"
            :formatter="item.formatter"
            align="center"
          />
        </el-table>
      </dtDialog>
    </div>
  </div>
</template>

<script>
import unifiedReportingApi from '@/api/investment/unifiedReporting'
import DialogForm from '@/views/investment/components/DialogForm'
import commonList from "@/utils/mixins/investment/commonList";
const statesList = [{value: 0,label:'待审核'}, {value: 1,label:'审核通过'}, {value: 2,label:'未通过'}]
export default {
  name: 'PreferentialPolicy',
  components: {
    DialogForm
  },
  dicts: ['zs_zyyt', 'zs_ctjd'],
  mixins: [commonList],
  data() {
    const _this = this
    return {
      filterForm: {
        premisesName: '',
        premisesStreetCode: '',
        premisesAreaMin: null,
        premisesAreaMax: null
      },
      fileName: '楼宇数据导入',
      handleImportTemplate: unifiedReportingApi.downloadLY,
      importApi: unifiedReportingApi.importLY,
      updateStatusApi: unifiedReportingApi.updateStatusLY,
      statesList,
      // 列信息
      columns: [
        { prop: 'premisesStreetCode', label: `街道`, width: '160px', formatter: function(row) {
            return _this.dict.label.zs_ctjd[row.premisesStreetCode] || ''
          } },
        { prop: 'premisesName', label: `楼宇名称`, minWidth: '180px' },
        { prop: 'location', label: `详细位置`, minWidth: '180px' },
        { prop: 'premisesArea', label: `楼宇总面积(m²)`, width: '160px' },
        { prop: 'premisesIdle', label: `闲置楼层面积(m²)`, width: '160px' },
        { prop: 'premisesFormatCode', label: `适宜招引业态`, width: '120px', formatter: function(row) {
            return _this.dict.label.zs_zyyt[row.premisesFormatCode] || ''
          } },
        { prop: 'contactsName', label: `联系人`, width: '100px' },
        { prop: 'contactsPhone', label: `联系电话`, width: '120px' },
        { prop: 'status', label: `审核状态`, width: '120px', formatter: function(row) {
            let info = _this.statesList.filter(v => v.value === row.status)
            let style = row.status === 1 ? '' : !row.status ? 'color: #1289ff;' : 'color: #ff0000;'
            return <span style={style}>{!!info.length ? info[0].label : ''}</span>
          } },
        { prop: 'cause', label: `审核原因`, minWidth: '180px' }
      ],
      formDesignList: [],
      addDesignList: [
        { type: 'text', prop: 'premisesName', label: `楼宇名称`, rules: [{ required: true, message: '请输入楼宇名称', trigger: 'blur' }] },
        { type: 'select', prop: 'premisesStreetCode', label: `街道`, dictTypeCode: 'zs_ctjd', rules: [{ required: true, message: '请选择街道', trigger: 'change' }] },
        { type: 'text', prop: 'location', label: `详细位置`, rules: [{ required: true, message: '请输入位置', trigger: 'blur' }] },
        { type: 'number', prop: 'premisesArea', label: `楼宇总面积(m²)`, rules: [{ required: true, message: '请输入楼宇总面积', trigger: 'blur' }] },
        { type: 'number', prop: 'premisesIdle', label: `闲置楼层面积(m²)`, rules: [{ required: true, message: '请输入闲置楼层面积', trigger: 'blur' }] },
        { type: 'textarea', prop: 'premisesIdleDetail', label: `闲置楼层面积描述`, maxlength: 450, autosize: {minRows: 4, maxRows: 10} },
        { type: 'text', prop: 'contactsName', label: `联系人`, rules: [{ required: true, message: '请输入联系人', trigger: 'blur' }] },
        { type: 'text', prop: 'contactsPhone', label: `联系电话`, rules: [{ required: true, message: '请输入联系电话', trigger: 'blur' }, { pattern: /^(1[3456789]\d{9})$/, message: '请输入有效的联系电话', trigger: 'blur' }] },
        { type: 'select', prop: 'premisesFormatCode', label: `适宜招引业态`, dictTypeCode: 'zs_zyyt', rules: [{ required: true, message: '请选择适宜招引业态', trigger: 'change' }] },
        { type: 'text', prop: 'locationLon', label: `经度`, disabled: true, rules: [{ required: true, message: '请选择经度', trigger: 'change' }] },
        { type: 'text', prop: 'locationLat', label: `纬度`, disabled: true, rules: [{ required: true, message: '请选择纬度', trigger: 'change' }] },
        { type: 'textarea', prop: 'remark', label: `描述`, maxlength: 450, autosize: {minRows: 4, maxRows: 10} },
      ],
      editDesignList: [
        { type: 'textarea', prop: 'causeUpdate', label: `修改原因`, maxlength: 500, autosize: {minRows: 3, maxRows: 6}, rules: [{ required: true, message: '请输入修改原因', trigger: 'blur' }] },
      ],
      shFormDesignList: [
        { type: 'select', prop: 'status', label: `审核状态`, dictOptions: statesList.slice(1), rules: [{ required: true, message: '请选择审核状态', trigger: 'change' }] },
        { type: 'textarea', prop: 'cause', label: `原因`, maxlength: 500, autosize: {minRows: 3, maxRows: 6}, rules: [] },
      ],
      idKeyName: 'premisesId',
      editRecordColumns: [
        { prop: 'premisesStreetCode', label: `街道`, width: '160px', formatter: function(row) {
            return _this.dict.label.zs_ctjd[row.premisesStreetCode] || ''
          } },
        { prop: 'premisesName', label: `楼宇名称`, minWidth: '180px' },
        { prop: 'location', label: `详细位置`, minWidth: '180px' },
        { prop: 'premisesArea', label: `楼宇总面积(m²)`, width: '160px' },
        { prop: 'premisesIdle', label: `闲置楼层面积(m²)`, width: '160px' },
        { prop: 'premisesFormatCode', label: `适宜招引业态`, width: '120px', formatter: function(row) {
            return _this.dict.label.zs_zyyt[row.premisesFormatCode] || ''
          } },
        { prop: 'contactsName', label: `联系人`, width: '100px' },
        { prop: 'contactsPhone', label: `联系电话`, width: '120px' },
        { prop: 'remark', label: `描述`, minWidth: '180px' },
        { prop: 'causeUpdate', label: `修改原因`, minWidth: '180px' },
      ],
      editRecordList: [],
      isOpenRecordPop: false,
      formData: {},
      dialogOpenType: ''
    }
  },
  activated() {
  },
  methods: {
    // 获取页面列表
    getList() {
      this.$_getList(unifiedReportingApi.getLYList, {
        ...this.filterForm,
        ...this.MX_pageNav
      })
    },
    // 编辑按钮
    handleEdit(data) {
      let type = 'update'
      this.dialogOpenType = type
      this.formDesignList = [...this.addDesignList, ...this.editDesignList]
      this.$refs.dialogForm.open(type, unifiedReportingApi.updateLYItem, data || this.MX_applicationMsg)
    },
    // 添加按钮
    handleAdd() {
      let type = 'add'
      this.dialogOpenType = type
      this.formDesignList = this.addDesignList
      this.$refs.dialogForm.open(type, unifiedReportingApi.addLYItem)
    },
    // 删除按钮
    handleDelete(id) {
      this.$_handleDelete(unifiedReportingApi.delLYItem, [id], `${this.idKeyName}s`)
    },
    /** 下载按钮操作 */
    handleExport() {
      this.$_handleDownload(this.handleImportTemplate, `${this.fileName}模板`);
    },
    //审核
    handleReview(data){
      let type = 'review'
      this.dialogOpenType = type
      this.formDesignList = this.shFormDesignList
      this.$refs.dialogForm.open(type, unifiedReportingApi.updateLYItem, { [this.idKeyName]: data[this.idKeyName] })
    },
    getEditRecordList(id){
      const loading = this.$loading({ lock: true, text: `查询中...` })
      unifiedReportingApi.getLYEditHistoryList({
        [this.idKeyName]: id,
        pageSize: -1
      }).then((res) => {
        this.isOpenRecordPop = true
        this.editRecordList = res.data.rows || []
        loading.close()
      }).catch((r) => {
        loading.close()
      })
    },
    getFormData(info){
      this.formData = info
      if (this.dialogOpenType === 'review' && info.hasOwnProperty('status')){
        if (info.status === 2){
          this.$set(this.formDesignList[1], 'rules', [{ required: true, message: '请输入原因', trigger: 'blur' }])
        }else {
          this.$set(this.formDesignList[1], 'rules', [])
        }
      }
    },
    otherParamsCheck(){
      let flag = true
      if (this.formData.premisesArea < this.formData.premisesIdle){
        this.$dtMessageBox({ title: '提示', message: '闲置楼层面积(m²)需小于楼宇总面积(m²)，请重新设置！', type: 'warning' })
        flag = false
      }
      return flag
    }
  }
}
</script>

<style lang="scss" scoped>
.tBtn{
  margin-top: 3px;
  margin-bottom: 3px;
}
</style>
